import 'dart:convert';

import 'package:dio/dio.dart' as dio;
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import '../../helper/global/user/logic.dart';
import '../../helper/route/routes.dart';
import '../../helper/utils/httputils/net_api.dart';
import '../../helper/utils/httputils/urlpath.dart';
import '../../helper/utils/storage.dart';
import 'state.dart';

class PasswordloginLogic extends GetxController {
  final PasswordloginState state = PasswordloginState();

  @override
  void onInit() async {
    await SpUtil().init().then((value) {
      String phone = SpUtil().getStr('phone');

      state.nameCotroller.text = phone;
    });
    super.onInit();
  }

  //勾选
  void check() {
    state.isChecked = !state.isChecked;
    update();
  }

  //登录接口

  login() async {
    // print(SpUtil().getStr(key_store_user_token));
    // print(SpUtil().getJSON(key_store_user));
    // return;

    // if (!state.isSlided) {
    //   EasyLoading.showToast('请拖动滑块');
    //   return;
    // }

    if (!state.isChecked) {
      EasyLoading.showToast('请阅读并同意《用户协议》和《隐私政策》');
      return;
    }

    String loginName = state.nameCotroller.text;
    String password = state.passCotroller.text;

    var params = {
      'loginName': loginName,
      'loginPassword': password,
      'loginType': '1',
      'appCode': 'A20230407113124312',
    };

    if (state.loginType == 0) {
      String loginPassword = state.passCotroller.text;
      if (loginName.trim().isEmpty) {
        return EasyLoading.showToast('请输入账号');
      }

      if (loginPassword.trim().isEmpty) {
        return EasyLoading.showToast('请输入密码');
      }
      // params['loginPassword'] = loginPassword;
    } else {
      params['tenantId'] = 'TENANT_GZ';
      params['phone'] = loginName;
      String smCode = state.smsCotroller.text;
      if (smCode.trim().isEmpty) {
        return EasyLoading.showToast('请输入验证码');
      }
      params['code'] = smCode;
    }

    var res = state.loginType == 0
        ? await Api.login(params)
        : await Api.smslogin(params);
    if (res.isSuccess) {
      await UserController().loginSuccess(res.data);
      Get.back(result: true);
    } else {
      EasyLoading.showToast(res.msg);
    }
  }

  Future<bool> sendSms() async {
    if (GetUtils.isPhoneNumber(state.nameCotroller.text)) {
      var params = {
        'userId': -1,
        'postId': -1,
        'userName': -1,
        'appCode': "A20211202140446825",
        'oneParam': state.nameCotroller.text,
        'type': "cryun",
        'tenantId': 'TENANT_GZ',
        'check': '0',
        'phone': state.nameCotroller.text,
      };
      var response = await Api.sendCode(params);
      if (response.isSuccess) {
        return true;
      } else {
        EasyLoading.showToast(response.msg);
      }

      return false;
    } else {
      EasyLoading.showToast('请输入正确的手机号码');
      return false;
    }
  }

  //发送验证码接口
  sendCode(String phone) async {}

  changeLoginType() {
    state.loginType = state.loginType == 1 ? 0 : 1;

    update();
  }

  //显示或隐藏密码
  fixPasswordText() {
    state.isShow = !state.isShow;
    update();
  }
}
